package com.example.demo.controller;

import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import java.util.function.BiConsumer;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserMapper userMapper;


    @GetMapping("/list")
    @Transactional
    public List<User> list () {
        List<User> list = userMapper.list();
        return list;
//        throw new RuntimeException("occur exception");
    }


    public static void main (String[] args) throws Exception {
        User zhangsan = new User("zhangsan", "123456");
        Supplier<String> supplier = zhangsan::getUsername;
        System.out.println(supplier.get());
        Function<User, String> getUsername = User::getUsername;
        Class<? extends Function> aClass = getUsername.getClass();
        System.out.println(aClass);

        System.exit(0);

    }

    /**
     * 对象赋值
     * User user = new User();
     * consumer use user::set...
     */
    public static <V> void setFieldValue (Consumer<V> consumer, V value) {
        consumer.accept(value);
        consumer.andThen(consumer);
    }

    /**
     *
     * 对象赋值
     * biConsumer use User::set..
     */
    public static <T, V> void setFieldValue (BiConsumer<T, V> biConsumer, T obj, V value) {
        biConsumer.accept(obj, value);
        biConsumer.andThen(biConsumer);
    }

    /**
     * 获取对象属性值
     * function use User::get..
     */
    public static <T, R> R getFieldValue (Function<T, R> function, T obj) {
        return function.apply(obj);
    }

}
